<?php

namespace Api\Controller;

use Think\Controller;

class ReturnOrderController extends PublicController
{

    /**
     * 用户提交退款申请
     */
    public function index()
    {
        $uId = intval($this->get("id"));
        if (empty($uId)) {
            $this->json_error("网络错误！");
            exit;
        }
        $order_id = intval($_GET['order_id']);
        $order = M("order")->where("id=" . $order_id)->find();
        if ($order['status'] == 10) {
            $this->json_error("改订单不符合退款！");
            exit;
        }
        if (IS_POST) {
            // 查看退款次数有没有超过2次
            $backTimes = M('return_order')->where('order_id=' . $order['id'])->field('id')->select();
            if (count($backTimes) > 2) {
                $this->json_error("outOfTimes");
                exit;
            }
            $data = array();
            $data['reason'] = htmlentities($_POST['reason']);
            $data['describe'] = htmlentities($_POST['describe']);
            $data['imgs'] = $_POST['imgs'];
            $data['addtime'] = time();
            $data['status'] = 1;
            $data['order_id'] = $order['id'];
            $data['user_id'] = $order['uid'];
            $data['order_sn'] = $order['order_sn'];
            $data['refund_money'] = $order['amount'];
            $result = M("return_order")->add($data);
            if ($result) {
                $ff['status'] = 52;
                $ff['back'] = 1;
                M("order")->where("id=" . $order_id)->setField($ff);
                $this->json_result("申请成功！");
                exit;
            } else {
                $this->json_error("申请失败");
                exit;
            }
        } else {
            //获取退款金额
            $price = $order['amount'];
            $this->json_result($price);
        }

    }

    /**
     * 售后服务列表
     * @author tiedong
     * @date
     */
    public function returnList()
    {
        $uId = intval($this->get("id"));
        if (empty($uId)) {
            $this->json_error("网络错误！");
            exit;
        }
        $result = M("return_order")->where("user_id=$uId")->order('id desc')->select();
        $map = [];
        foreach ($result as $key => $value) {
            // 增加一个筛选，去除多条退款申请，只显示最新的
            if (isset($map[$value['order_id']])) {
                unset($result[$key]);
                continue;
            } else {
                $map[$value['order_id']] = 1;
                if ($value['status'] == -2) {
                    unset($result[$key]);
                    continue;
                }
            }

            $result[$key]['order_status'] = M("order")->where("id=" . $value['order_id'])->getField("status");
            $bi_id = M("order")->where("id=" . $value['order_id'])->getField("vid");
            $result[$key]['brand_image'] = __DATAURL__ . "UploadFiles/" . M("brand_image")->where('id=' . $bi_id)->getField("brand_photo");
            $fff = M("order_product")->where("order_id=" . $value['order_id'])->field("name,price,num")->select();
            $result[$key]['product'] = $fff;
        }
        $this->json_result($result);
        exit;

    }

    /**
     * 上传图片接口
     * @author tiedong
     * @date 2018-01-26
     */
    public function uploadFile()
    {
        $file = $_FILES['file'];
        $info = $this->upload_images($file, array('jpg', 'png', 'jpeg', 'gif'), "returnOrder/" . date(Ymd));
        $file_path = 'UploadFiles/' . $info['savepath'] . $info['savename'];
        $ret = array(
            'file_path' => $file_path
        );
        $this->json_result($ret);

    }

    /**
     * 取消退款申请
     */
    public function cancelBack()
    {
        $uId = intval($this->get("id"));
        if (empty($uId)) {
            $this->json_error("网络错误！");
            exit;
        }

        $backId = intval($_GET['back_id']);
        if (!$backId) {
            $this->json_error("请输入退款id！");
            exit;
        }

        $returnInfo = M('return_order')->where('id=' . $backId)->find();

        if (!$returnInfo) {
            $this->json_error("退款记录不存在！");
            exit;
        }

        if (M('return_order')->where('id=' . $backId)->save(['status' => -2])) {
            M('order')->where('id=' . $returnInfo['order_id'])->save(['status' => 40, 'back' => 0]);
            $this->json_result([], '退款申请已取消');
        } else {
            $this->json_error("数据更新失败！");
            exit;
        }
    }
}

?>